package org.automated.realestatemanager.advertisementgetter;

import org.apache.log4j.Logger;
import org.automated.realestatemanager.parsers.AdvertisementParser;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class Avers extends AbstractAdvertisementGetter {

	private static final Logger log = Logger.getLogger(Gorod.class);

	public void executeParcing(AdvertisementParser advParser) {

		final int pages = 74;

		try {
			Parser parser = null;
			for (int k = 0; k < pages; k++) {
				parser = new Parser(
						"http://www.avers.in.ua/flat?page="
								+ k
								+ "&filter0[447]=447&filter1=&filter2=&filter3=&filter4=&filter5=&filter6=&filter7=&filter8=&filter11=&filter12=&filter10[138]=138");

				parser.setEncoding("utf-8");

				System.out.println("Searching...");

				final NodeFilter atrb2 = new CssSelectorNodeFilter(".odd");
				NodeList nodeList2 = parser.parse(atrb2);

				for (int i = 0; i < nodeList2.size(); i++) {
					final Node node = nodeList2.elementAt(i);
					String n = node.toHtml();
					n = n.replaceAll("<[^>]*>", " ");
					n = n.replaceAll("[\\s]{2,}", " ");
					log.debug("\n\n" + n);
					advParser.executeParcing(n);
				}

			}
		} catch (final ParserException e) {
			e.printStackTrace();
		}

	}
}
